<template>
	<div>
		<router-link to="/" tag="div" class="header-abs" v-show="showAbs">
			<div class="iconfont header-abs-back">&#xe624;</div>
		</router-link>
		<div class="header-fixed" v-show="!showAbs" :style="opacityStyle">
			景点详情
			<router-link to="/">
				<div class="iconfont header-fixed-back">&#xe624;</div>
			</router-link>
		</div>
	</div>
</template>

<script>
export default {
  name:'DetailHeader',
  data() {
    return {
      showAbs:true,
      opacityStyle:{
        opacity:0
      }
    }
  },
  methods: {
    // 触发的函数，当监听器计算出top大于60，显示图标，隐藏返回按键
    handleScroll(){
      const top = document.documentElement.scrollTop
      if (top > 60 ) {
        let opacity = top / 140
        opacity = opacity > 1 ? 1 :opacity
        this.opacityStyle = {
          opacity
        }
        this.showAbs = false
      }else {
        this.showAbs = true
      }
      // console.log(document.documentElement.scrollTop)
    }
  },
  // 当页面进入活跃的时候，触发该生命函数
  activated() {
    // 添加事件监听器，滚动事件，触发函数this.handleScroll ,注意是全局的绑定，一定要解绑！
    window.addEventListener('scroll',this.handleScroll)
  },
  //离开活跃页，触发该函数
  deactivated() {
    //解绑函数
    window.removeEventListener('scroll',this.handleScroll)
  },
}
</script>

<style lang="stylus">
@import '~styles/varibles.styl';

.header-abs {
  z-index: 2;
  position: absolute;
  left: 0.2rem;
  top: 0.2rem;
  width: 0.8rem;
  height: 0.8rem;
  line-height: 0.8rem;
  border-radius: 0.4rem;
  text-align: center;
  background-color: rgba(0, 0, 0, 0.8);

  .header-abs-back {
    color: #ffffff;
    font-size: 0.4rem;
  }
}

.header-fixed {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: $headerHeight;
  line-height: $headerHeight;
  text-align: center;
  color: red;
  background: $bgColor;
  font-size: 0.32rem;

  .header-fixed-back {
    position: absolute;
    width: 0.64rem;
    text-align: center;
    font-size: 0.4rem;
    top: 0;
    left: 0;
    color: #fff;
  }
}
</style>